﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;

namespace CPPEI.Coolzon.UI.PetroleumDataPartLib
{
    public class OGCMapLayer : TiledMapServiceLayer
    {
        private const double cornerCoordinate = 20037508.3427892;
        private string _url;
        private string _type;
        public OGCMapLayer(string url, string type)
            : base()
        {
            _url = url;
            _type = type;
        }
        public override void Initialize()
        {
            ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
            this.FullExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892)
            {
                SpatialReference = new SpatialReference(102100)
            };
            //图层的空间坐标系
            this.SpatialReference = new SpatialReference(102100);
            // 建立切片信息，每个切片大小256*256px，共16级.
            this.TileInfo = new TileInfo()
            {
                Height = 256,
                Width = 256,
                Origin = new MapPoint(-cornerCoordinate, cornerCoordinate) { SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100) },
                Lods = new Lod[18]
            };
            //为每级建立方案，每一级是前一级别的一半.
            double resolution = cornerCoordinate * 2 / 256;
            for (int i = 0; i < TileInfo.Lods.Length; i++)
            {
                TileInfo.Lods[i] = new Lod() { Resolution = resolution };
                resolution /= 2;
            }
            // 调用初始化函数
            base.Initialize();
        }

        public override string GetTileUrl(int level, int row, int col)
        {
            string url = _url + "?c=" + col + "&" + "r=" + row + "&" + "l=" + level + "&type=" + _type;
            return string.Format(url);
        }
    }
}